package com.cn.kabalifemerchants.ui.page.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.cn.kabalifemerchants.R;
import com.cn.kabalifemerchants.base.IBaseActivity;
import com.cn.kabalifemerchants.config.LoginHelper;
import com.cn.kabalifemerchants.event.RxBus;
import com.cn.kabalifemerchants.event.RxBusBean;
import com.cn.kabalifemerchants.ui.login.bean.MsgBean;
import com.cn.kabalifemerchants.ui.page.bean.CaseDetailsBean;
import com.cn.kabalifemerchants.ui.page.bean.CaseListBean;
import com.cn.kabalifemerchants.utils.GlideUtil;
import com.cn.kabalifemerchants.utils.RxToast;
import com.kongzue.dialog.v2.SelectDialog;
import com.makeramen.roundedimageview.RoundedImageView;
import com.zzhoujay.richtext.ImageHolder;
import com.zzhoujay.richtext.RichText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by 晓林 on 2020/9/11.
 * function: 服务案例详情
 */
public class ServiceCaseDetailsActivity extends IBaseActivity<ServiceCaseListView, ServiceCaseListPresenter> implements ServiceCaseListView {


    Bundle bundle;
    int id;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.img_cover)
    RoundedImageView imgCover;
    @BindView(R.id.tv_details)
    TextView tvDetails;


    int sign = 0;//标识一下。1：是删除

    @Override
    protected ServiceCaseListPresenter createPresenter() {
        return new ServiceCaseListPresenter();
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_service_case_details;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setTitleBar("案例详情");
        bundle = getIntent().getExtras();
        id = bundle.getInt("id");

        mPresenter.onCaseDetailsData(LoginHelper.getLonginData().getResult(), id);

    }

    @Override
    public void onReLoggedIn(String msg) {
        RxToast.error(msg);
        LoginHelper.againGoToLoginActivity(getMe());
    }

    @Override
    public void onError(String msg) {
        RxToast.error(msg);
    }

    @Override
    public Context _getContext() {
        return null;
    }

    @Override
    public void onCaseListSuccess(CaseListBean bean) {

    }

    @Override
    public void onCaseDetailsSuccess(CaseDetailsBean bean) {
//        tvTitle.setText(bean.getResult().getTitle());
//        GlideUtil.ImageLoad(this, bean.getResult().getCover(), imgCover);
//        RichText.fromHtml(bean.getResult().getContent()).into(tvDetails);
        RichText.fromHtml(bean.getResult().getContent()).autoFix(true).showBorder(false).autoPlay(true).scaleType(ImageHolder.ScaleType.center_crop) // 图片缩放方式
                .size(ImageHolder.MATCH_PARENT, ImageHolder.WRAP_CONTENT).into(tvDetails);
    }

    @Override
    public void onCaseSetUpSuccess(MsgBean bean) {
        RxToast.success(bean.getMsg());
        if (sign == 0)
            mPresenter.onCaseDetailsData(LoginHelper.getLonginData().getResult(), id);
        else {
            finish();
            RxBus.getDefault().post(new RxBusBean(20001, 20001));
        }
    }


    @OnClick({R.id.tv_btn_next_delete, R.id.tv_btn_next_no, R.id.tv_btn_next_set})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_btn_next_delete:
                SelectDialog.show(getMe(), "提示", "确定要删除吗？", "确认", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        sign = 1;
                        mPresenter.onCaseSetUpData(LoginHelper.getLonginData().getResult(), id, "del");
                    }
                }, "取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });

                break;
            case R.id.tv_btn_next_no:
                sign = 0;
                mPresenter.onCaseSetUpData(LoginHelper.getLonginData().getResult(), id, "cancel");
                break;
            case R.id.tv_btn_next_set:
                sign = 0;
                mPresenter.onCaseSetUpData(LoginHelper.getLonginData().getResult(), id, "top");
                break;
        }
    }
}
